package poo.vehicule;



import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class LectureEcritureVehicules {
	public static final String fichier = "vehicule.txt";
    private static ArrayList<Vehicule> readVehicules(Scanner in){
	    ArrayList<Vehicule> listeVehicule = new ArrayList();
	    while(in.hasNext()){
	    	Vehicule v1 = new Vehicule();
	        v1.readData(in);
	        listeVehicule.add(v1);
	    }
	    in.close();
	    return listeVehicule;
    }
      
	private static void writeVehicules(ArrayList<Vehicule> listeVehicule, PrintWriter out){
		for(Vehicule v1 : listeVehicule){
			v1.writeData(out);
	    }
	}
	
	 public static void main(String[] args) {
         
         ArrayList<Vehicule> listeVehicule = new ArrayList();
         Vehicule v1=new Vehicule(1,"Citroen","C1",2010,0,10000,5);
         listeVehicule.add(v1);
         Vehicule v2=new Vehicule(2,"Citroen","DS3",2012,10000,13000,2);
         listeVehicule.add(v2);
         Vehicule v3=new Vehicule(3,"Peuget","306",2008,5000,13000,10);
         listeVehicule.add(v3);      
         PrintWriter pr=null;
         writeVehicules(listeVehicule,pr);
         ArrayList<Vehicule> listeVehiculeLecture;
         File f = new File (fichier);
         FileReader fr;
         try {
                 fr = new FileReader (f);
                 BufferedReader br = new BufferedReader (fr);
                 Scanner scanner = new Scanner (br);
                 listeVehiculeLecture = readVehicules(scanner);
                 for(Vehicule tmp : listeVehiculeLecture){
                         System.out.println(tmp);
                 }
         }
         catch (FileNotFoundException e) {
                 e.printStackTrace();
         }
	 }
	 
}
